'''

Case Type： 数据库系统
Case Name： 数据库启动前，pg_hba.conf文件无读权限，重启数据库
Create At:  2020/06/24
Owner    :  @li-xin12345
Descption:  1.数据库启动前，去除pg_hba.conf文件的读权限；2.重新启动数据库

history：
created： 2020/06/28  created by @li-xin12345
'''

import os, unittest, time
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant


class PortOccupied(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case098.py start------------')
        self.rootNode = Node('PrimaryRoot')
        self.primaryNode = Node(node='PrimaryDbUser')
        self.SOURCE_PATH = macro.DB_ENV_PATH
        self.PG_HBA_FILE_NAME = os.path.join(macro.DB_INSTANCE_PATH, macro.PG_HBA_FILE_NAME)
        self.Constant = Constant()

    def test_set_w_stop(self):
        # 关闭正在运行的数据库
        self.log.info('------------------关闭正在运行的数据库------------------------------')
        set_cmd = f'''
           source {self.SOURCE_PATH};
           gs_om -t stop;
                 '''
        rm_msg = self.primaryNode.sh(set_cmd).result()
        self.log.info(set_cmd)
        self.log.info(rm_msg)
        res1 = rm_msg.find(self.Constant.STOP_SUCCESS_MSG) > -1
        self.assertTrue(res1)

        # 去除pg_hba.conf文件的读权限
        self.log.info('------------------去除pg_hba.conf文件的读权限------------------------------')
        set_cmd = f'''
                   chmod u-r {self.PG_HBA_FILE_NAME};
                         '''
        rm_msg = self.primaryNode.sh(set_cmd).result()
        self.log.info(set_cmd)
        self.log.info(rm_msg)

        # 启动数据库
        self.log.info('------------------启动数据库------------------------------')
        set_cmd = f'''
                   source {self.SOURCE_PATH};
                   gs_om -t start;
                         '''
        rm_msg = self.primaryNode.sh(set_cmd).result()
        self.log.info(set_cmd)
        self.log.info(rm_msg)
        self.assertTrue(rm_msg.find(self.Constant.START_SUCCESS_MSG) == -1)

        # 恢复pg_hba.conf文件的读权限
        self.log.info('------------------恢复pg_hba.conf文件的读权限------------------------------')
        recover_cmd = f'chmod u+r {self.PG_HBA_FILE_NAME};'
        recover_msg = self.primaryNode.sh(recover_cmd).result()
        self.log.info(recover_cmd)
        self.log.info(recover_msg)

        # 再次启动数据库
        self.log.info('------------------再次启动数据库------------------------------')
        set_cmd = f'''
                   source {self.SOURCE_PATH};
                   gs_om -t start;
                         '''
        rm_msg = self.primaryNode.sh(set_cmd).result()
        self.log.info(set_cmd)
        self.log.info(rm_msg)
        res1 = rm_msg.find(self.Constant.START_SUCCESS_MSG) > -1
        self.assertTrue(res1)

    def tearDownTestCase(self):
        self.log.info('----------------------------后置处理-----------------------------')
        db_status = self.sh_primy.getDbClusterStatus('status')
        self.log.info(db_status)
        if db_status:
            pass
        else:
            try:
                is_started = self.sh_primy.startDbCluster()

                self.assertTrue(is_started)
                self.log.info(f'{is_started}')
                self.log.info(f'db_status: {is_started}')
            finally:
                recover_cmd = f'chmod u+r {self.PG_HBA_FILE_NAME};'
                recover_msg = self.primaryNode.sh(recover_cmd).result()
                self.log.info(recover_cmd)
                self.log.info(recover_msg)
        self.log.info('------------------reliability_dbsys098 执行完成------------------------------')
